//
//  UIBarButton.swift
//  JHSinaWeibo
//
//  Created by apple on 16/5/21.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
extension UIBarButtonItem{

//给函数的参数设置默认值
    //函数的参数默认值如果不传递就直接使用
    convenience init(imageName:String? = nil,title:String? = nil,target:AnyObject?,action:Selector){
    self.init()
    let btn = UIButton()
        //设置图片
        if let img = imageName{
        
            btn.setImage(UIImage(named: img), forState: .Normal)
            btn.setImage(UIImage(named: img + "_highligheted"), forState: .Highlighted)
        
        }
       
        //添加事件
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        btn.setTitle(title, forState: .Normal)
        //设置颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        // 设置成自适应大小
        btn.sizeToFit()
        //设置成自定义的按钮，即给它保成ButtonItem
        self.customView = btn
    }


}